package com.reverie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reverie.common.pojo.CommonDeleteParam;
import com.reverie.common.pojo.CommonResult;
import com.reverie.entity.Advert;
import com.reverie.entity.Recommend;
import com.reverie.service.IAdvertService;
import com.reverie.service.IRecommendService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home")
@AllArgsConstructor
public class HomeController {
    private final IAdvertService advertService;
    private final IRecommendService recommendService;

    @PostMapping("/recommend/list")
    public CommonResult<List<Recommend>> getList(){
        QueryWrapper<Advert> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("re_sort");
        return CommonResult.data(recommendService.list());
    }

    @PostMapping("/recommend/delete")
    public CommonResult<Boolean> delete(@RequestBody CommonDeleteParam commonDeleteParam){
        return CommonResult.data(recommendService.removeById(commonDeleteParam.getId()));
    }

    @PostMapping("/recommend/save")
    public CommonResult<Boolean> save(@RequestBody Recommend recommend){
        return CommonResult.data(recommendService.saveOrUpdate(recommend));
    }

    @PostMapping("/advert/list")
    public CommonResult<List<Advert>> getAdvertList(){
        QueryWrapper<Advert> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("ad_sort");
        return CommonResult.data(advertService.list(wrapper));
    }

    @PostMapping("/advert/delete")
    public CommonResult<Boolean> deleteAdvert(@RequestBody CommonDeleteParam commonDeleteParam){
        return CommonResult.data(advertService.removeById(commonDeleteParam.getId()));
    }

    @PostMapping("/advert/save")
    public CommonResult<Boolean> saveAdvert(@RequestBody Advert advert){
        return CommonResult.data(advertService.saveOrUpdate(advert));
    }

}
